package com.core.cloundbusinessapi.system.device.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * @author =====Lqj=====
 * @className UserDeviceDTO
 * @description 用户设备信息DTO
 * @create 2024/12/16 15:00
 **/
@Getter
@Setter
public class UserDeviceDTO {

    /**
     * 设备ID
     */
    private String deviceId;

    /**
     * 设备名称
     */
    private String deviceName;

    /**
     * 设备类型
     */
    private String deviceType;

    /**
     * 设备类型代码
     */
    private Integer deviceTypeCode;

    /**
     * 登录IP
     */
    private String loginIp;

    /**
     * 登录地址
     */
    private String loginLocation;

    /**
     * 浏览器
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 用户代理
     */
    private String userAgent;

    /**
     * 登录时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date loginTime;

    /**
     * 最后活跃时间
     */
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date lastActiveTime;

    /**
     * Token
     */
    private String token;

    /**
     * 是否当前设备
     */
    private Boolean isCurrent;

    /**
     * 是否在线
     */
    private Boolean isOnline;

    /**
     * 会话时长（分钟）
     */
    private Long sessionDuration;

    /**
     * 设备状态：0-离线，1-在线，2-异常
     */
    private Integer status;

    /**
     * 构造函数
     */
    public UserDeviceDTO() {
        this.isCurrent = false;
        this.isOnline = false;
        this.status = 0;
    }

    /**
     * 便捷构造函数
     */
    public UserDeviceDTO(String deviceId, String deviceName, String deviceType, 
                        String loginIp, String token) {
        this();
        this.deviceId = deviceId;
        this.deviceName = deviceName;
        this.deviceType = deviceType;
        this.loginIp = loginIp;
        this.token = token;
        this.loginTime = new Date();
        this.lastActiveTime = new Date();
        this.isOnline = true;
        this.status = 1;
    }

    /**
     * 设置设备类型信息
     */
    public void setDeviceTypeInfo(Integer typeCode) {
        this.deviceTypeCode = typeCode;
        switch (typeCode) {
            case 0:
                this.deviceType = "Web端";
                break;
            case 1:
                this.deviceType = "iOS端";
                break;
            case 2:
                this.deviceType = "安卓端";
                break;
            case 3:
                this.deviceType = "Windows端";
                break;
            case 4:
                this.deviceType = "macOS端";
                break;
            case 5:
                this.deviceType = "鸿蒙端";
                break;
            default:
                this.deviceType = "未知设备";
                break;
        }
    }

    /**
     * 更新活跃时间
     */
    public void updateActiveTime() {
        this.lastActiveTime = new Date();
        this.isOnline = true;
        this.status = 1;
    }

    /**
     * 设置离线状态
     */
    public void setOffline() {
        this.isOnline = false;
        this.status = 0;
    }

    /**
     * 计算会话时长
     */
    public void calculateSessionDuration() {
        if (this.loginTime != null && this.lastActiveTime != null) {
            long duration = (this.lastActiveTime.getTime() - this.loginTime.getTime()) / (1000 * 60);
            this.sessionDuration = Math.max(0, duration);
        }
    }

    /**
     * 检查设备是否活跃
     * @param timeoutMinutes 超时分钟数
     * @return true-活跃，false-不活跃
     */
    public boolean isActive(int timeoutMinutes) {
        if (this.lastActiveTime == null) {
            return false;
        }
        
        long now = System.currentTimeMillis();
        long lastActive = this.lastActiveTime.getTime();
        long timeout = timeoutMinutes * 60 * 1000L;
        
        return (now - lastActive) < timeout;
    }

    /**
     * 获取设备简要信息
     */
    public String getDeviceInfo() {
        StringBuilder info = new StringBuilder();
        if (deviceName != null && !deviceName.isEmpty()) {
            info.append(deviceName);
        } else {
            info.append(deviceType != null ? deviceType : "未知设备");
        }
        
        if (os != null && !os.isEmpty()) {
            info.append(" (").append(os).append(")");
        }
        
        return info.toString();
    }

    /**
     * 获取登录信息摘要
     */
    public String getLoginSummary() {
        StringBuilder summary = new StringBuilder();
        summary.append("设备: ").append(getDeviceInfo());
        
        if (loginIp != null) {
            summary.append(", IP: ").append(loginIp);
        }
        
        if (loginLocation != null && !loginLocation.isEmpty()) {
            summary.append(", 地址: ").append(loginLocation);
        }
        
        if (loginTime != null) {
            summary.append(", 登录时间: ").append(loginTime);
        }
        
        return summary.toString();
    }
}
